package com.summer.campussct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.campussct.mapper.BannerMapper;
import com.summer.campussct.pojo.Banner;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.service.IBannerService;
import com.summer.campussct.utils.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {

    @Resource
    private FileUtil fileUtil;


    @Value("${file-position.bannerPath}")
    private String bannerPath;

    @Override
    public RestResult getBannerByCategoryId(Integer categoryId, Integer campusOrShopType) {
        List<Banner> list = this.list(
                new LambdaQueryWrapper<Banner>()
                        .eq(categoryId != null ,Banner::getCategoryId, categoryId)
                        .eq(campusOrShopType != null ,Banner::getCampusOrShopType, campusOrShopType)
                        .select(Banner::getId, Banner::getPicture, Banner::getLinkUri));
        return RestResult.success(list);
    }

    @Override
    public RestResult addBanner(Banner banner) {
        Long categoryId = banner.getCategoryId();
        Integer campusOrShopType = banner.getCampusOrShopType();
        if (categoryId == null  && campusOrShopType == null) throw new RuntimeException("至少关联分类id或车商校园");
        // 转存图片
        String newUrl = fileUtil.transferSave(banner.getPicture(), bannerPath);
        banner.setPicture(newUrl);

        if (!this.save(banner)) {
            throw new RuntimeException("新增轮播图失败");
        }
        return null;
    }

    @Override
    public RestResult getDetails(long id) {
        Banner banner = this.getOne(new LambdaQueryWrapper<Banner>()
                .select(Banner::getLinkUri)
                .eq(Banner::getId, id));
        return RestResult.success(banner.getLinkUri());
    }

    @Override
    public RestResult modifyBanner(Banner banner) {
        //2. 转存图片
        String newUrl = fileUtil.transferSave(banner.getPicture(), bannerPath);
        banner.setPicture(newUrl);
        //3. 修改
        if (!this.updateById(banner)) {
            throw new RuntimeException("修改banner失败");
        }
        return RestResult.success();
    }

    @Override
    public RestResult deleteBanner(long id) {
        Banner banner = this.getOne(new LambdaQueryWrapper<Banner>()
                .select(Banner::getPicture)
                .eq(Banner::getId, id));
        // 删除banner
        if (banner == null || !this.removeById(id)) {
            throw new RuntimeException("删除banner失败");
        }
        // 删除图片
        try {
            fileUtil.deleteFile(banner.getPicture());
        } catch (IOException e) {
            System.err.println("删除图片失败");
            throw new RuntimeException(e);
        }
        return RestResult.success();
    }
}
